package com.yunfeng.supercall.app;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.view.View;
import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.lin.utils.Net;
import com.ly.quickdev.library.app.BaseAppContext;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.yunfeng.supercall.BuildConfig;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.contactsutils.ContactsHelper;
import com.yunfeng.supercall.manager.ManagerFactory;
import com.yunfeng.supercall.manager.SuperFriendsManager;

public class AppContext extends BaseAppContext {
	public LocationClient mLocationClient = null;
	public BDLocationListener myListener = new MyLocationListener();
	public static Bitmap BITMAP = null;
	// 定位地址
	public static String addr;
	// 经纬度
	public static double lat;
	public static double lng;

	@Override
	public void onCreate() {
		super.onCreate();
		/** 初始化极光推送 */
		//		JPushInterface.setDebugMode(BuildConfig.DEBUG);
		JPushInterface.init(this);
		/** 初始化百度地图和定位 */
		SDKInitializer.initialize(getApplicationContext());
		mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
		mLocationClient.registerLocationListener(myListener); // 注册监听函数
		LocationClientOption option = new LocationClientOption();
		//		option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
		//		option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		//		option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
		mLocationClient.setLocOption(option);
		mLocationClient.start();
		/** 默认头像bitmap */
		BITMAP = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_default_user_avatar);
		/** 网络图片资源的地址头部 */
		IMG_TOP = "http://superchat.res.yunfengapp.com:8887/";
		/** 初始化T9搜索 */
		ContactsHelper.getInstance(getApplicationContext());
		/** 初始化好友管理器 */
		((SuperFriendsManager) ManagerFactory.createManager(SuperFriendsBean.class))
		.init(this).getList();

		ShareSDK.initSDK(this);


		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
		.build();
		ImageLoader.getInstance().init(config);
		if(Net.IsNetworkAvailble(getApplicationContext()) ) {
			ImageLoader.getInstance().clearDiscCache();
			ImageLoader.getInstance().clearDiskCache();
			ImageLoader.getInstance().clearMemoryCache();	    
		}
		/*   AssetManager mgr=getAssets();//得到AssetManager
	    Typeface tf=Typeface.createFromAsset(mgr, "fonts/ttf.ttf");//根据路径得到Typeface
	    tv.setTypeface(tf);//设置字体
		 */	}

	public static void displayImgWithDefault(View iv, String url) {
		if (url != null && url.startsWith("http")) {
			sFinalBitmap.display(iv, url, BITMAP, BITMAP);
		} else {
			sFinalBitmap.display(iv, IMG_TOP + url, BITMAP, BITMAP);
		}
	}

	public static void displayImgWithDefault1(View iv, String url) {
		displayImgWithDefault(iv, url);
		// sFinalBitmap.display(iv, url, BITMAP, BITMAP);
	}

	public static void displayChatImg(View iv, String url) {
		sFinalBitmap.display(iv, url);
	}

	public static void displayLocalImg(View iv, String url) {
		sFinalBitmap.display(iv, url);
	}

	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;
			addr = location.getAddrStr();
			lat = location.getLatitude();
			lng = location.getLongitude();
			mLocationClient.stop();
		}

		@Override
		public void onReceivePoi(BDLocation arg0) {
			// TODO Auto-generated method stub

		}

	}
}
